clear 
est clear

**********************************************************************
* First Stage/Compliance
**********************************************************************



*** Prep data  *********************

use "$data\SMU_CleanData_Panel_Public", replace
sort cr_id location_branch location_club round

keep if round==0 

* merge in log data, weirdly we are missing 5 CRs...
merge 1:1 cr_id using "${data}\CashTherapy_log.dta", assert(match master)
tab _merge randomization

* label treatment nicely 	
label define randomization 0 "Control" 1 "IPT-G" 2 "IPT-G+", modify
label values randomization randomization 
rename randomization treat
tab treat  

* clean up attendance
replace attended_atleast1=. if attended_atleast1<0

* mean attendance and CT received 
tabstat attended_atleast1 CT_status, by(treat) statistics(mean n)

* label outcomes 
label var attended_atleast1 "Therapy,att.ed at least one"
label var CT_status "Received Cash"

*** create baseline controls *********************
*** Age 
su CR_age, d
* create age var called age for convenience 
drop age 
gen age = CR_age 

*** ever pregnant 
fre s_eversex
fre f_everpreg
tab f_everpreg s_eversex, m 
* ever pregnant is zero if never had sex 
gen everpregnant = 0 		if f_everpreg==0 | s_eversex==0
replace everpregnant = 1 	if f_everpreg==1  
* is missing in 51 cases, due to either sex or preg Q's missing 
tab everpregnant
mdesc everpregnant

*** ever married
*	count `as if' married as married
*	means divorced/separated/widowed are not married... 
fre ms_currentlymarried
gen evermarried = inlist(ms_currentlymarried,1,2,3,4,5,6) if !missing(ms_currentlymarried) & ms_currentlymarried>=0
tab ms_currentlymarried evermarried, m


*** list of controls 
local controls age everpregnant evermarried PPI_score phq8_score
su `controls'
mdesc `controls' 

*** for missings, replace with median and create dummy indicating missingness 
foreach v of varlist everpregnant evermarried {
	di "var: `v'"
	* missing indicator 
	gen mis_`v' = missing(`v')
	tab mis_`v'
	* replace with median
	qui su `v' , d
	replace `v' = r(p50) if mis_`v'==1 
	* check 
	tab `v' mis_`v'
}

*** Center controls 
foreach v of local controls {
	qui su `v', meanonly
	replace `v' = `v'-r(mean)
}
su `controls'

*** Fix up labels 
label var PPI_score 		"PPI"
label var age 				"Age"
label var everpregnant 		"Ever pregnant"
label var evermarried 		"Ever married"
label var phq8_score 		"PHQ-8"


keep cr_id treat block club attended_atleast1 tot_attendance CT_status `controls'


*** Regress *************************

label var attended_atleast1 "Attended therapy"

local ys attended_atleast1 CT_status

local ctrls age everpregnant evermarried PPI_score phq8_score 

foreach y of local ys {
	
	*** Unadjusted *******************
	* Linear Model: treatment, Cluster SEs at Club level
	eststo `y':	reg `y'		b0.treat,  vce(cluster club)
		*** add scalars
		* control mean
		qui su `y' if treat==0  
		estadd scalar c_mean = r(mean) 	
		* p-value 
		test 1.treat == 0 
		estadd scalar pval_ipt = r(p)
		test 2.treat == 0 
		estadd scalar pval_iptplus = r(p)
		test 1.treat == 2.treat 
		estadd scalar pval_eq = r(p)
		testparm 1.treat 2.treat 
		estadd scalar pval_both = r(p) 
		* first stage Fstat
		estadd scalar fstat = e(F) 
}




* simple 
#delimit ;
esttab  attended_atleast1 CT_status  
	using "${tables}/compliance.tex", replace booktabs
	label b(3) se(3) nostar nonote nobaselev drop(_cons) compress
	stats(c_mean N  pval_ipt pval_iptplus pval_eq, fmt(3 0 3 3 3 3 )  
	label("Control mean" "Obs." "H0: IPT-G=0" "H0: IPT-G+=0" "H0: IPT-G = IPT-G+")) 
	;
#delimit cr


